/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pneuservis.backend.services.impl;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import org.apache.shiro.authz.annotation.RequiresRoles;
import pneuservis.backend.dao.CustomerDAOLocal;
import pneuservis.backend.dao.OrderPneuDAOLocal;
import pneuservis.backend.entities.Customer;
import pneuservis.backend.entities.OrderPneu;
import pneuservis.backend.others.Secured;
import pneuservis.backend.services.OrderPneuServiceLocal;
import pneuservis.backend.to.CustomerTO;
import pneuservis.backend.to.OrderPneuTO;

/**
 *
 * @author Admin
 */
@Stateless(name="OrderPneuService", mappedName="OrderPneuService")
public class OrderPneuService implements OrderPneuServiceLocal {

    @EJB
    private OrderPneuDAOLocal orderPneuDAO;
    @EJB
    private CustomerDAOLocal customerDAO;
    
    @Override
    public void createOrderWithCustomer(OrderPneuTO orderPneu){
        Customer c = orderPneu.getCustomer().toCustomer();
        customerDAO.createCustomer(c);
        OrderPneu o = orderPneu.toOrderPneu();
        o.setCustomer(c);
        orderPneuDAO.createOrder(o);
    }
    
    @Override
    public void createOrder(OrderPneuTO orderPneu) {
        orderPneuDAO.createOrder(orderPneu.toOrderPneu());
    }

    @Override
    public OrderPneuTO getOrder(Long id) {
        return new OrderPneuTO(orderPneuDAO.getOrder(id));
    }

    @Override
    public void updateOrder(OrderPneuTO orderPneu) {
        orderPneuDAO.updateOrder(orderPneu.toOrderPneu());
    }

    @Override
    public void removeOrder(OrderPneuTO orderPneu) {
        orderPneuDAO.removeOrder(orderPneu.toOrderPneu());
    }

    @Override
//    @Secured
//    @RequiresRoles("admin")
    public List<OrderPneuTO> getAllOrders() {
        List<OrderPneuTO> list = new ArrayList<>();
        for(OrderPneu o : orderPneuDAO.getAllOrders()) {
            list.add(new OrderPneuTO(o));
        }
        return list;
    }

    @Override
//    @Secured
//    @RequiresRoles("admin")
    public List<OrderPneuTO> getOrdersByCustomer(CustomerTO customer) {
        List<OrderPneuTO> list = new ArrayList<>();
        for(OrderPneu o : orderPneuDAO.getOrdersByCustomer(customer.toCustomer())) {
            list.add(new OrderPneuTO(o));
        }
        return list;
    }

}
